﻿//--------------------------------------------------------------------- 
//THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY 
//KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE 
//IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A 
//PARTICULAR PURPOSE. 
//---------------------------------------------------------------------
using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Controls;
using Microsoft.Drawing;
using System.IO;
using PDAMain.DAL;
using System.Runtime.InteropServices;
using PDAMain.Entity;
using PDAMain.Interface;

namespace PDAMain.Forms.InspectionWork
{
    public partial class InspectionList : BaseForm
    {
        private Brush selectedFontBrush;
        private Brush fontBrush;
        private Font fontBold;
        private Font fontRegular;

        public InspectionList()
        {
            InitializeComponent();

            InitResources();
        }

        private void InitResources()
        {
            this.selectedFontBrush = new SolidBrush(Color.White);
            this.fontBrush = new SolidBrush(Color.Black);
            this.fontBold = new Font("Tahoma", 9, FontStyle.Bold);
            this.fontRegular = new Font("Tahoma", 9, FontStyle.Regular); 
        }

        private void PopulateList(DataTable dt)
        {
            columnHeader.Width = listView.Width - 17;
            foreach (DataRow dr in dt.Rows) {
                this.fillItem(
                    dr["ASSIGNMENTID"].ToString(), 
                    dr["ASSETNUM"].ToString(), 
                    "（" + dr["STATUS"].ToString() + "）", 
                    dr["LOCDESCRIPTION"].ToString());
            }
            
        }


        private void fillItem(string assignmentId, string text, string type, string location)
        {
            ListViewItem item = new ListViewItem(assignmentId);
            
            ListViewItem.ListViewSubItem textItem = new ListViewItem.ListViewSubItem();
            textItem.Text = text;
            item.SubItems.Add(textItem);
            
            ListViewItem.ListViewSubItem typeItem = new ListViewItem.ListViewSubItem();
            typeItem.Text = type;
            item.SubItems.Add(typeItem);

            ListViewItem.ListViewSubItem locationItem = new ListViewItem.ListViewSubItem();
            locationItem.Text = location;
            item.SubItems.Add(locationItem);
            
            this.listView.Items.Add(item);
        }

       


        private void listView_DrawItem(object sender, DrawListViewItemEventArgs e)
        {
            if (e.Item.Selected)
            {
                Rectangle rect = new Rectangle(1, e.Bounds.Top + 2, e.Bounds.Width - 12, e.Bounds.Height - 3);

                e.Graphics.DrawGradientRoundedRectangle(
                    rect,
                    Color.FromArgb(132, 178, 222),
                    Color.FromArgb(0, 101, 189),
                    Color.FromArgb(0, 101, 189),
                    new Size(8, 8)
                );
            }
            else
            {
                e.Graphics.FillRectangle(this.selectedFontBrush, e.Bounds);
                //e.Graphics.DrawString(e.Item.Text, new Font("Tahoma", 20, FontStyle.Regular), this.selectedFontBrush, e.Bounds.Left, e.Bounds.Top);
            }

            this.DrawItemCustom(e);

            if (e.ItemIndex > 0)
            {
                int lineY = e.Bounds.Top;
                e.Graphics.DrawLine(new Pen(Color.DarkGray), e.Bounds.Left, lineY, e.Bounds.Width, lineY);
            }
        }


        private void DrawItemCustom(DrawListViewItemEventArgs e)
        {
            SizeF textSize = e.Graphics.MeasureString(e.Item.SubItems[1].Text, fontBold);           

            int top = 6;
            int left = e.Bounds.Left + 10;
            int displayWidth = e.Bounds.Width - 17;

            // Prepare text rectangles
            Rectangle itemTextRect = new Rectangle(left, e.Bounds.Top + top, (int)textSize.Width, (int)textSize.Height);

            int typeItemLeft = left + (int)textSize.Width + 10;
            Rectangle typeItemTextRect = new Rectangle(typeItemLeft, e.Bounds.Top + top, displayWidth - (int)textSize.Width, (int)textSize.Height);

            top += (int)textSize.Height + 4;
            Rectangle locationItemTextRect = new Rectangle(left, e.Bounds.Top + top, displayWidth, (int)textSize.Height);                        

            // Draw text for items and sub items
            if (!e.Item.Selected)
            {
                e.Graphics.DrawString(e.Item.SubItems[1].Text, fontBold, fontBrush, itemTextRect);
                e.Graphics.DrawString(e.Item.SubItems[2].Text, fontRegular, fontBrush, typeItemTextRect);
                e.Graphics.DrawString(e.Item.SubItems[3].Text, fontRegular, fontBrush, locationItemTextRect);              
            }
            else
            {
                e.Graphics.DrawString(e.Item.SubItems[1].Text, fontBold, selectedFontBrush, itemTextRect);
                e.Graphics.DrawString(e.Item.SubItems[2].Text, fontRegular, selectedFontBrush, typeItemTextRect);
                e.Graphics.DrawString(e.Item.SubItems[3].Text, fontRegular, selectedFontBrush, locationItemTextRect);
            }
          
            // Draw an image
            //e.Graphics.DrawImageAlphaChannel((IImage)e.Item.Tag, 4, e.Bounds.Top + 2);
        }
        
        private void listView_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            MessageBox.Show(this.listView.Items[e.Index].SubItems[0].Text);
        }

        private void RepairWorkList_Load(object sender, EventArgs e)
        {
            try
            {
                if (!OpenBarCode())
                {
                    MessageBox.Show("打开扫描接口失败，请重登系统");
                }
                EnableBeeper(true);
                EnableVibrater(true);

                DataSet ds = DataHelper.InspectionWorkDao.list();

                if (ds != null && ds.Tables.Count > 0)
                    this.PopulateList(ds.Tables[0]);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        /// <summary>
        /// 匹配 
        /// </summary>
        /// <param name="strBC"></param>
        /// <returns></returns>
        private bool mearchData(string strBC)
        {
            foreach (ListViewItem item in this.listView.Items)
            {
                string assetNum = item.SubItems[1].Text;
                if (assetNum == strBC)
                {
                    DataHelper.InspectionWorkDao.AssignmentId = item.Text;
                    DataHelper.InspectionWorkDao.AssetNum = assetNum;
                    return true;
                }
            }
            return false;
        }


        // 重载 基类BaseForm的这个方法
        public override void OnBarCodeNotify(byte[] BarCodeData, int nLength)
        {
            EnableBarCode(false);

            string BarCode = Encoding.Default.GetString(BarCodeData, 0, nLength);
            //strBarCode = BarCode;
            EnableBarCode(true);

            bool Flag = mearchData(BarCode);
            if (Flag)
            {
                Identity.StartTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                InspectionDetail formShow = new InspectionDetail();
                formShow.Show();
                this.Close();/**/
                CloseBarCode();
            }
        }
        /// <summary>
        /// 同步数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuItemSyncData_Click(object sender, EventArgs e)
        {
            if (NetWorkState.checkWifiState() || NetWorkState.checkGprsState())
            {
                listView.Items.Clear();
                DataSet ds = DataHelper.InspectionWorkDao.list();
                if (ds != null && ds.Tables.Count > 0)
                    this.PopulateList(ds.Tables[0]);
                MessageBox.Show("同步成功");
            }
            else
            {
                MessageBox.Show("网络未连接");
            }
        }
        /// <summary>
        /// 返回
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuItemGoBack_Click(object sender, EventArgs e)
        {
            FuntionView fun = new FuntionView();
            fun.Show();
            this.Close();

        }
        /// <summary>
        /// 简单查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuItemSampleQuery_Click(object sender, EventArgs e)
        {
            InspectionSelect formShow = new InspectionSelect();
            formShow.Show();
            this.Close();
         
        }

      

        private void RepairWorkList_Closed(object sender, EventArgs e)
        {
            // 关闭BarCode扫描设备
            CloseBarCode();
        }
      
    }
}